package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import adapter.PinPaiAdapter;
import yztc.com.shiyuanbaoyou.R;

/**
 * Created by My on 2016/9/22.
 */
public class PinPaiTeMaiFragment extends Fragment {
    private String[] titles={"最新上线","昨日上新","最后疯抢","精致女装","鞋包配饰","居家生活","美妆个护","美食零食","母婴用品","数码家电","精品男装","文体用品"};
    private List<Fragment> fragments;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_pinpaitemai,null);
        tabLayout= (TabLayout) view.findViewById(R.id.pinpai_tab);
        viewPager= (ViewPager) view.findViewById(R.id.vp_pinpai);
        initData();
        initVP();
        return view;
    }

    private void initVP() {
        fragments=new ArrayList<>();

        ZuiXinFragment f1=new ZuiXinFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("id",1);
        f1.setArguments(bundle);

        ZuiXinFragment f2=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",2);
        f2.setArguments(bundle);

        ZuiXinFragment f3=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",3);
        f3.setArguments(bundle);

        ZuiXinFragment f4=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",4);
        f4.setArguments(bundle);

        ZuiXinFragment f5=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",5);
        f5.setArguments(bundle);

        ZuiXinFragment f6=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",6);
        f6.setArguments(bundle);

        ZuiXinFragment f7=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",7);
        f7.setArguments(bundle);

        ZuiXinFragment f8=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",8);
        f8.setArguments(bundle);

        ZuiXinFragment f9=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",9);
        f9.setArguments(bundle);

        ZuiXinFragment f10=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",10);
        f10.setArguments(bundle);

        ZuiXinFragment f11=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",11);
        f11.setArguments(bundle);

        ZuiXinFragment f12=new ZuiXinFragment();
        bundle=new Bundle();
        bundle.putInt("id",12);
        f11.setArguments(bundle);

        fragments.add(f1);
        fragments.add(f2);
        fragments.add(f3);
        fragments.add(f4);
        fragments.add(f5);
        fragments.add(f6);
        fragments.add(f7);
        fragments.add(f8);
        fragments.add(f9);
        fragments.add(f10);
        fragments.add(f11);
        fragments.add(f12);


        PinPaiAdapter adapter=new PinPaiAdapter(getFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
    }

    private void initData() {
        for (int i=0;i<titles.length;i++){
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });




    }
}
